package com.danny.Controller;

import com.danny.mapper.FloorMapper;
import com.danny.pojo.Floor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class FloorController {

    @Autowired
    FloorMapper floorMapper;

    /**
     * 查询所有楼层信息
     * @return List
     * */
    @GetMapping("/queryAllFloors")
    public List<Floor>queryAllFloors(){
        return floorMapper.queryAllFloors();
    }

    /**
     * 无参数分页查询
     * @param page
     * @param pageSize
     * @return List
     * */
    @GetMapping("/queryFloorsLimit")
    public List<Floor>queryFloorsLimit(int page,int pageSize){
        HashMap<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("pageSize",pageSize);
        return floorMapper.queryFloorsLimit(map);
    }

    /**
     * 有参数的分页模糊查询(似乎不是很有必要)
     * @param floorInfo
     * @param page
     * @param pageSize
     * @return List
     * */
    @GetMapping("/queryFloorsLimitHasInput")
    public List<Floor> queryFloorsLimitHasInput(String floorInfo,int page,int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        map.put("floorInfo",floorInfo);
        map.put("page",page);
        map.put("pageSize",pageSize);

        return floorMapper.queryFloorsLimitHasInput(map);
    }

    /**
     * 查询楼层信息条数
     * @return int
     * */
    @GetMapping("/floorsCount")
    public int floorsCount(){
        return floorMapper.floorsCount();
    }

    /**
     * 模糊查询满足条件的楼层信息条数
     * @return int
     * */
    @GetMapping("/floorsCountHasInput")
    public int floorsCountHasInput(String floorInfo){
        return floorMapper.floorsCountHasInput(floorInfo);
    }

    /**
     * 增加一个楼层信息
     * @param floor
     * */
    @GetMapping("/addFloor")
    public String addFloor(Floor floor){
        List<Floor> list = queryAllFloors();
        for (Floor f:
             list) {
            if(floor.getFloorNo()==(f.getFloorNo())
                    || floor.getFloorInfo().equals(f.getFloorInfo()))
                return "inserted";
        }
        if(floorMapper.addFloor(floor)==0) {
            return "error";
        }
        return "inserted";
    }

    /**
     * 根据floor中楼层id修改楼层信息
     * @param floor
     *
     * */
    @GetMapping("/updateFloor")
    public String updateFloor(Floor floor){
        if(floorMapper.updateFloor(floor)==0) {
            return "error";
        }
        return "inserted";
    }

    /**
     * 根据id删除楼层信息
     * @param id
     * */
    @GetMapping("/deleteFloor")
    public String deleteFloor(int id){
        if(floorMapper.deleteFloorById(id)==0){
            return "error";
        }
        return "deleted";
    }

}
